package com.me.jingdongshop.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.Category;
import com.me.jingdongshop.model.bean.HomeRecommendedBrand;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.ui.activity.MainActivity;
import com.me.jingdongshop.ui.activity.ProductlistActivity;
import com.me.jingdongshop.ui.view.CircleImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Tm on 2017/5/5.
 */

public class GoodsGridViewAdapter extends BaseAdapter{
    private List<Category.CategoryBean> data;
    private Context mContext;

    public GoodsGridViewAdapter(List<Category.CategoryBean> datas, Context context) {
        this.data = datas;
        this.mContext=context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
           holder = new ViewHolder();

            convertView = LayoutInflater.from(mContext).inflate(R.layout.category_rigth_grid, parent, false);
            holder.name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.icon= (CircleImageView) convertView.findViewById(R.id.circle);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();

        }

        //设置数据
        holder.name.setText(data.get(position).getName());
        String url = IHttpService.HOST_URL+data.get(position).getPic();
        Picasso.with(mContext).load(url).into(holder.icon);
            //点击跳转页面
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent((MainActivity)mContext, ProductlistActivity.class);
                mContext.startActivity(intent);

            }
        });

        return convertView;
    }
    class ViewHolder
    {
        public TextView name;
        public CircleImageView icon;
    }
}
